﻿using System;
using Team4Mock.Entity;
using System.Data;

namespace Team4Mock.UserControl.Programme
{
    public partial class ViewAll : System.Web.UI.UserControl
    {
        ProgrammeEntity pe=new ProgrammeEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["role"])))
                {
                    if (Session["role"].ToString().Equals("Admin"))
                    {
                        
                        btnDel.Visible = true;
                    }
                    else
                    {
                        btnDel.Visible = false;
                    }
                }
                else
                {
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alertMe();", true);
                    Response.Redirect("/FormView/Default.aspx");
                    btnDel.Visible = false;
                }

                if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["ID"])))
                {
                    pnView.Visible = false;
                    pnct.Visible = true;
                    int id = int.Parse(Request.QueryString["ID"]);
                    pe.ProgramId = id;
                    DataTable dt=new DataTable();
                    dt = pe.GetById(pe);
                    txtDes.Text = dt.Rows[0][2].ToString();
                    txtProName.Text = dt.Rows[0][1].ToString();
                    idContact.Value = dt.Rows[0][3].ToString();
                    if (dt.Rows[0][4].ToString()=="Yes")
                    {
                        ckisactive.Checked = true;
                    }
                    else
                    {
                        ckisactive.Checked = false;
                    }
                }
                else
                {
                    pnView.Visible = true;
                    pnct.Visible = false;
                    LoadGV();
                }
            }

        }
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            pnView.Visible = false;
            pnct.Visible = true;
        }
        void LoadGV()
        {
            gvProgramme.DataSource = pe.GetDataTrue();
            gvProgramme.DataBind();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Programme.aspx");
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alertMe();", true);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //Amend
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                if (txtProName.Text=="")
                {
                    tb.Text = "Input ProgramName";
                }
                else
                {
                    int id = int.Parse(Request.QueryString["ID"]);
                    pe.ProgramId = id;
                    pe.ProgramName = txtProName.Text;
                    pe.Contact = int.Parse(idContact.Value);
                    pe.Description = txtDes.Text;
                    pe.IsActive = ckisactive.Checked ? 1 : 0;
                    pe.UpdateData(pe);
                    tb.Text = "Update Success";
                }
            }
            //Add
            else
            {
                if (txtProName.Text == "")
                {
                    tb.Text = "Input ProgramName";
                }
                else
                {
                    if (idContact.Value == "")
                    {
                        tb.Text = "Select Contact";
                    }
                    else
                    {
                        pe.ProgramName = txtProName.Text;
                        pe.Contact = int.Parse(idContact.Value);
                        pe.Description = txtDes.Text;
                        pe.IsActive = ckisactive.Checked ? 1 : 0;
                        pe.InsertData(pe);
                        tb.Text = "Insert Success";
                    }
                }
            }
        }

        protected void All_Click(object sender, EventArgs e)
        {
            LoadGV();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            pe.KeyWord = "ABCDE";
            gvProgramme.DataSource = pe.Search(pe);
            gvProgramme.DataBind();
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            pe.KeyWord = "FGHIJ";
            gvProgramme.DataSource = pe.Search(pe);
            gvProgramme.DataBind();
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            pe.KeyWord = "KMNL";
            gvProgramme.DataSource = pe.Search(pe);
            gvProgramme.DataBind();
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            pe.KeyWord = "OPQR";
            gvProgramme.DataSource = pe.Search(pe);
            gvProgramme.DataBind();
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            pe.KeyWord = "STUV";
            gvProgramme.DataSource = pe.Search(pe);
            gvProgramme.DataBind();
        }

        protected void LinkButton7_Click(object sender, EventArgs e)
        {
            pe.KeyWord = "WXYZ";
            gvProgramme.DataSource = pe.Search(pe);
            gvProgramme.DataBind();
        }

        protected void ckactive_CheckedChanged(object sender, EventArgs e)
        {
            if (ckactive.Checked==true)
            {
                gvProgramme.DataSource = pe.GetData();
                gvProgramme.DataBind();
            }
            else
            {
                gvProgramme.DataSource = pe.GetDataTrue();
                gvProgramme.DataBind();
            }
            
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(Request.QueryString["ID"]);
                pe.ProgramId = id;
                pe.Del(pe);
                Response.Redirect("/FormView/Programme.aspx");
            }
            catch
            {
                tb.Text = "Error while being processed";
            }
        }
    }
}